package com.example.helloandroid;

import java.util.HashMap;
import java.util.Locale;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
//import android.widget.TextView;
//import android.widget.Toast;

public class HelloAndroid extends Activity implements OnInitListener 
{
	/** Called when the activity is first created. */
	private static final int MY_DATA_CHECK_CODE = 112428;
	private static AudioManager mh_AudMgr       = null;
	private static TextToSpeech mh_TTS          = null;
	
	private static HashMap<String, String> m_TalkerHash=new HashMap<String, String>(1);
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	
    	m_TalkerHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
    	
    	try
    	{
			mh_TTS    = new TextToSpeech(this,this);
			mh_AudMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    	}
    	catch (ActivityNotFoundException Ex)
    	{
    		WriteText(Ex.toString());
    	}
    }
    
    protected void OnActivityResult ( int requestCode, int resultCode, Intent data )
    {
    	WriteText ( "in OnActivityResult");
    	if ( MY_DATA_CHECK_CODE == requestCode )
    	{
    		if ( TextToSpeech.Engine.CHECK_VOICE_DATA_PASS == resultCode )
    		{
    			WriteText ("TTS available");
    			mh_TTS = new TextToSpeech(this,this);
    		}
    		else
    		{
    			/* Install the TTS data */
    			WriteText ("TTS NOT available.Installing");
    			Intent installIntent = new Intent();
    			installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    			startActivity(installIntent);
    		}
    	}
     }
    
	public void onInit(int status) 
	{
		WriteText ("TTS init complete");
		mh_TTS.setLanguage(Locale.UK);
	}
	
	public void onDestroy ()
	{
		mh_TTS.shutdown();
		super.onDestroy();		
	}
    
    private void WriteText (String msg)
    {
    	Log.i ( "HelloAndroid", msg);
    }
    
    public static TextToSpeech GetTTSHandle()
    {
    	return mh_TTS; 
    }
    
    public static AudioManager GetAudioMgrHdl()
    {
    	return mh_AudMgr; 
    } 
    
    public static HashMap<String,String> GetAudioHashMap()
    {
    	return m_TalkerHash;
    }
}
